<?php 
abstract class Controller extends CController
{	
	const T_MESSAGE = 'message';
	const T_ERROR = 'error';
	const T_NOTICE = 'notice';
	
	protected static $noLogin = array(
		'index' => array('verify', 'error'), 
		'users' => array('login', 'logout'),
		'test' => array('index', 's')
	);
	
	protected $options = null;
	
	protected static $t = array(self::T_MESSAGE, self::T_ERROR, self::T_NOTICE);
	
	public function _init()
	{
		require FRAMEWORK_DIR . '/helpers/CDbSession.php';
		session_start();
		$c = CControllerConfig::getController();
		$a = CControllerConfig::getAction();
		if (!(isset(self::$noLogin[$c]) && in_array($a, self::$noLogin[$c])))
		{
			if (isset($_SESSION['admin']))
				$admin = $_SESSION['admin'];
			else
				$this->forward(array(), 'login', 'users');
			
			if ((!$admin instanceof UsersModel) || empty($admin->user_id))
				$this->forward(array(), 'login', 'users');
		}
		
		$this->loginUsers();
	}
	
	public function loginUsers()
	{
		$sql = 'SELECT COUNT(session_id) FROM ' . CConfig::get('config', 'session_save_table') . ' WHERE user_id > ?';
		$this->view->login_users = $this->dbStatement->fetchColumnOneBySql($sql, 0);
	}
	
	public function getOptionsDir()
	{
		return APP_DIR . DS . 'data' . DS . 'options.inc.php';
	}
	
	public function &getOptions()
	{
		if (null === $this->options)
			$this->options = require $this->optionsDir;
		
		return $this->options;
	}
	
	public function postInitView()
	{
		$view = $this->getView();
		// 公共文件夹目录.
		$view->commonPath = $view->publicPath . '/common';
		// 后台文件夹目录.
		$view->adminPath = $view->publicPath . '/administrator';
	}
	
	public function preDisplay()
	{
		$this->view->m = trim($this->getRequest()->getParam('m', ''));
		$t = trim($this->getRequest()->getParam('t', ''));
		if (!in_array($t, self::$t))
			$t = self::T_MESSAGE;
		
		$this->view->t = $t;
	}
	
	public function loadModel($id = null, $model = null)
	{
		if (null === $model)
			$model = CControllerConfig::getController();
		
		$model = ucfirst($model) . CModelConfig::getModelKey();
		if (null === $id)
			$id = $this->getRequest()->getParam('id', 0);
		
		$id = (int) $id;
		if ($id <= 0)
			throw new CHttpException(404, '无效的ID');
		
		$model = new $model();
		$model->findByPk($id);
		if (!$model)
			throw new CHttpException(404, '无效的ID');
		
		return $model;
	}
	
	public function getPagelist($totalResults, $perPageResults = null)
	{
		if (null === $perPageResults)
		{
			$options = &$this->getOptions();
			$key = 'page_results_' . CControllerConfig::getController();
			if (isset($options[$key]))
				$perPageResults = $options[$key];
			else
				$perPageResults = $options['page_pages'];
		}
		
		$pageno = (int) $this->request->getParam('pageno', 1);
		if ($pageno <= 0)
			$pageno = 1;
		
		$limit = (int) $this->request->getParam('limit', $perPageResults);
		if ($limit < 0)
			$limit = $perPageResults;
		else if ($limit == 0)
			$limit = $totalResults;
			
		$pagelist = CPagelist::getInstance($pageno, $totalResults, $limit);
		$this->view->pageno = $pageno;
		$this->view->limit = $limit;
		$this->view->pagination = $pagelist->display();
		return $pagelist;
	}
	
	public function fckeditor($name, $toolbar, $value = '', $width = 820, $height = 550)
	{
		require_once ROOT_DIR . '/public/common/js/fckeditor/fckeditor.php';
		$fckeditor = new FCKeditor($name);
		$fckeditor->BasePath = $this->view->publicPath . '/common/js/fckeditor/';
		$fckeditor->Config['SkinPath'] = $this->view->publicPath . '/common/js/fckeditor/editor/skins/silver/';
		$fckeditor->Config['CustomConfigurationsPath'] = $this->view->publicPath . '/common/js/fck-definedbar.js';
		$fckeditor->ToolbarSet = $toolbar;
		$fckeditor->Width  = $width;
		$fckeditor->Height = $height;
		$fckeditor->Value  = $value;
		$this->view->fckeditor = $fckeditor->CreateHtml();
	}
	
	public function getUploadDir()
	{
		$filedir = ROOT_DIR . '/public/uploads/posts/' . date('Y');
		if (!is_dir($filedir))
			@mkdir($filedir, 0777);
		
		$filedir .= DS . date('md');
		if (!is_dir($filedir))
			@mkdir($filedir, 0777);
		
		
		return $filedir;
	}
}
